package cn.edu.jlu.community.app.sign;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;

import cn.edu.jlu.community.R;

public class SignupActivity extends Activity {
    private void signUp(String username , String password , String mobilePhoneNumber){
        AVUser user = new AVUser();
        user.setUsername(username);
        user.setMobilePhoneNumber(mobilePhoneNumber);
        user.setPassword(password);
        user.signUpInBackground(new SignUpCallback() {
            public void done(AVException e) {
                if (e == null) {
                    // successfully
                    Toast.makeText(SignupActivity.this, "signup succeeded", Toast.LENGTH_LONG).show();

//                    Intent intent = new Intent(SignupActivity.this, SignInActivity.class);
//                    SignupActivity.this.startActivity(intent);
                    SignupActivity.this.finish();
                } else {
                    // failed
                    Toast.makeText(SignupActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        findViewById(R.id.signup_submit_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("aaa", "signup submit button clicked") ;
                submit();
            }
        });

        findViewById(R.id.signup_clear_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reset() ;
            }
        });
    }

    private void submit(){
        EditText et = (EditText)findViewById(R.id.phoneEditText) ;
        String phone = et.getText().toString() ;
        et = (EditText)findViewById(R.id.passwordEditText) ;
        String password = et.getText().toString() ;
        et = (EditText)findViewById(R.id.confirmPassEditText) ;
        String confirmPassword = et.getText().toString() ;
        et = (EditText)findViewById(R.id.usernameEditText) ;
        String username = et.getText().toString() ;

        if( password.equals(confirmPassword)){
            signUp(username ,password ,  phone );
        }else{
            Toast.makeText(getApplicationContext(),"password don't match!",Toast.LENGTH_LONG).show() ;
            et = (EditText)findViewById(R.id.confirmPassEditText) ;
            et.setText("");
        }
    }
    private void reset(){
        EditText et = (EditText)findViewById(R.id.phoneEditText) ;
        et.setText("");
        et = (EditText)findViewById(R.id.passwordEditText) ;
        et.setText("");
        et = (EditText)findViewById(R.id.confirmPassEditText) ;
        et.setText("");
        et = (EditText)findViewById(R.id.usernameEditText) ;
        et.setText("");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_signup, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
